package com.isdust.jy.mbank.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.isdust.jy.mbank.App;
import com.isdust.jy.mbank.R;
import com.isdust.jy.mbank.fragment.Main;
import com.isdust.jy.mbank.fragment.forth;
import com.isdust.jy.mbank.fragment.second;
import com.isdust.jy.mbank.fragment.third;

import java.lang.reflect.Array;
import java.util.Arrays;

@SuppressWarnings("ALL")
public class Index extends FragmentActivity {

    private RadioGroup navGroup;
    private long exitTime = 0;
    protected App app;

    int widthPixels;

    String tabs[] = {"Main", "second", "add","third" , "forth"};
    private Fragment[] mFragments = new Fragment[5];
    private Class[] classes = {Main.class,second.class,second.class,third.class,forth.class};
    private Context mContext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;
        //获取屏幕宽度
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        widthPixels = displayMetrics.widthPixels;

        setContentView(R.layout.activity_tab);
        app = (App) getApplication();
        initView();
    }

    private void initView() {
        navGroup = (RadioGroup) findViewById(R.id.frames);
        RadioButton rbWeiHui = (RadioButton) findViewById(R.id.fram1);
        RadioButton rbAdd = (RadioButton) findViewById(R.id.fram2);
        RadioButton rbMine = (RadioButton) findViewById(R.id.fram3);
        RadioButton rbforth = (RadioButton) findViewById(R.id.fram4);
        RadioButton rbadd = (RadioButton) findViewById(R.id.framadd);
        //定义底部标签图片大小
        int dpi = (int) (widthPixels * 0.05f);
        Log.i("dpi", String.valueOf(dpi));
        Drawable drawableWeiHui = getResources().getDrawable(R.drawable.bottom_main);
        drawableWeiHui.setBounds(0, 0, dpi, dpi);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        rbWeiHui.setCompoundDrawables(null, drawableWeiHui, null, null);//只放上面

        Drawable drawableAdd = getResources().getDrawable(R.drawable.bottom_service);
        drawableAdd.setBounds(0, 0, dpi, dpi);
        rbAdd.setCompoundDrawables(null, drawableAdd, null, null);

        Drawable drawableRight = getResources().getDrawable(R.drawable.bottom_about);
        drawableRight.setBounds(0, 0, dpi, dpi);
        rbMine.setCompoundDrawables(null, drawableRight, null, null);

        Drawable drawableForth = getResources().getDrawable(R.drawable.bottom_about);
        drawableRight.setBounds(0, 0, dpi, dpi);
        rbforth.setCompoundDrawables(null, drawableRight, null, null);

        RadioButton radioButton = (RadioButton)findViewById(R.id.framadd);
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext,HabitatActivity.class));
            }
        });

        navGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                for (int i = 0; i < navGroup.getChildCount(); i++) {
                    if(i==2)
                    {
                        return;
                    }
                    else if (navGroup.getChildAt(i).getId() == checkedId) {
                        setTabSelection(i);
                        return;
                    }
                }
            }
        });
        RadioButton btn = (RadioButton) navGroup.getChildAt(0);
        btn.toggle();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 1000)  //System.currentTimeMillis()无论何时调用，肯定大于2000
            {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void setTabSelection(int index) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        for(int i=0;i<mFragments.length;i++){
            if(mFragments[i]==null && i!=2){
                try {
                    mFragments[i]= (Fragment) classes[i].newInstance();
                    transaction.add(R.id.content, mFragments[i]);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            //设置加号

        }
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        for (Fragment tmp : mFragments) {
            if (tmp != null)
                transaction.hide(tmp);
        }
        transaction.show(mFragments[index]);
        transaction.commit();
    }
}
